package com.accompany.controller;


import com.accompany.common.core.controller.BaseController;
import com.accompany.common.core.domain.AjaxResult;
import com.accompany.common.core.page.TableDataInfo;
import com.accompany.domain.pojo.TbCourse;
import com.accompany.domain.dto.CoursePageDto;
import com.accompany.service.impl.TbCourseService;
import com.accompany.validator.annotation.ValidateCourseRequest;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.HTML;
import java.util.List;

@RestController
@RequestMapping("/course")
@Slf4j
@Tag(name = "课程相关")
public class CourseController extends BaseController {

    @Autowired
    private TbCourseService tbCourseService;

    @GetMapping("/list")
    @Operation(summary = "分页查询课程")
    public TableDataInfo pageQuery(CoursePageDto coursePageDto) {
        log.info("分页查询：{}", coursePageDto);
        startPage();
        List<TbCourse> tbCoursePage = tbCourseService.pageQuery(coursePageDto);
        return getDataTable(tbCoursePage);
    }

    @GetMapping("/{id}")
    @Operation(summary = "查询课程详情")
    public AjaxResult getInfo(@PathVariable Long id) {
        log.info("查询课程详情：{}", id);
        TbCourse tbCourse = tbCourseService.getInfo(id);
        return AjaxResult.success(tbCourse);
    }


    @PostMapping
    @Operation(summary = "新增课程")
    public AjaxResult add(@RequestBody @ValidateCourseRequest TbCourse tbCourse) {
        log.info("新增课程：{}", tbCourse);
        boolean result = tbCourseService.add(tbCourse);
        return AjaxResult.success(result);
    }

    @PutMapping
    @Operation(summary = "修改课程")
    public AjaxResult edit(@RequestBody @ValidateCourseRequest TbCourse tbCourse) {
        log.info("修改课程：{}", tbCourse);
        boolean result = tbCourseService.edit(tbCourse);
        return AjaxResult.success(result);
    }

    @DeleteMapping("/{ids}")
    @Operation(summary = "批量删除课程")
    public AjaxResult deleteByIds(@PathVariable List<Long> ids) {
        log.info("删除课程：{}", ids);
        boolean result = tbCourseService.deleteByIds(ids);
        return AjaxResult.success(result);
    }


}
